<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>子组件向组附件传递数据</title>
</head>
<body>
<div id="app">
    <H1>{{ count }}</H1>
    <cpn v-bind:count="count" v-on:change="change"></cpn>
</div>

<template id="cpn">
    <button v-on:click="decrease">--</button>
    <button v-on:click="increase">++</button>
</template>

<script type="module">
    import {createApp, defineComponent, ref} from "../../../js/vue.esm-browser.js";

    let component = defineComponent(
        {
            template: "#cpn",
            props: {
                count: {
                    type: Number,
                    default: null,
                    required: true,
                }
            },
            emits: ['change'],
            setup(props, context) {
                let cn = ref(props.count)

                function decrease() {
                    cn.value = cn.value - 2
                    // console.log(cn.value);
                    context.emit('change', cn.value)
                }

                function increase() {
                    cn.value = cn.value + 2
                    // console.log(cn.value);
                    context.emit('change', cn.value)
                }

                return {
                    decrease, increase,
                }
            }
        }
    );

    let app = createApp(
        {
            setup() {

                let count = ref(0)

                function change(event) {
                    // console.log("get value: " + event);
                    count.value = event
                }

                return {
                    count, change,
                };
            }
        }
    );

    app.component("cpn", component)

    app.mount("#app")
</script>
</body>
</html>